<?php
/**
 * Created by PhpStorm.
 * User: yaoihu
 * Date: 2019/6/13
 * Time: 20:04
 */

namespace Shop\Http\Controllers\Wap;


use App\Helpers\SelfResponse;
use App\Models\Shop\UserAddressModel;
use Frontend\Models\UserBaseModel;
use Illuminate\Http\Request;
use Shop\Http\Controllers\BaseController;

class AddressController extends BaseController
{
    public function myAddress(Request $request)
    {

        $user_id = $request->input('user_id');
        $address = UserAddressModel::where('user_id',$user_id)->get();
        return $this->wapRender('address.show',[
            'page_title'=>'地址管理',
            'address'=>$address
        ]);
    }

    public function create(Request $request)
    {
        $user_id = $request->input('user_id');
        if($request->isMethod('post')){
            $validatedData = $request->validate([
                'phone'=>'required|mobile',
                'name'=>'required|text_rule',
                'is_default'=>'nullable',
                'address'=>'required|text_rule',
                'city'=>'required',
            ],[
                'required'=>':attribute不能为空',
                'mobile'=>':attribute格式不正确',
                'text_rule'=>':attribute存在非法字符',
            ],[
                'phone'=>'手机号',
                'name'=>'收货人',
                'is_default'=>'是否默认',
                'address'=>'详细地址',
                'city'=>'省市县',
            ]);
            $addressModel = new UserAddressModel();

            $addressModel->user_id = $user_id;
            $addressModel->consignee = $validatedData['name'];
            $addressModel->phone = $validatedData['phone'];
            $addressModel->area = $validatedData['city'];
            $addressModel->address = $validatedData['address'];
            $addressModel->is_default = 0;
            if(isset($validatedData['is_default'])){
                $addressModel->is_default = 1;
            }
            if($addressModel->save()){
                if($addressModel->is_default == 1){
                    $addressModel->where('user_id',$user_id)->where('id','!=',$addressModel->id)->where('is_default',1)->update(['is_default'=>0]);
                }
                return SelfResponse::success('添加地址成功','f_Wap_Address_myAddress');
            }

            return SelfResponse::error('添加地址失败');
        }else{
            return $this->wapRender('address.create_and_edit',[
                'page_title'=>'添加地址',
                'info'=>UserBaseModel::find($user_id)
            ]);
        }

    }

    public function update(Request $request,$addr_id)
    {
        $user_id = $request->input('user_id');
        if($request->isMethod('post')){
            $validatedData = $request->validate([
                'phone'=>'required|mobile',
                'name'=>'required|text_rule',
                'is_default'=>'nullable',
                'address'=>'required|text_rule',
                'city'=>'required',
            ],[
                'required'=>':attribute不能为空',
                'mobile'=>':attribute格式不正确',
                'text_rule'=>':attribute存在非法字符',
            ],[
                'phone'=>'手机号',
                'name'=>'收货人',
                'is_default'=>'是否默认',
                'address'=>'详细地址',
                'city'=>'省市县',
            ]);
            $addressModel = UserAddressModel::find($addr_id);

            if(!$addressModel||$addressModel->user_id != $user_id){
                return SelfResponse::error('编辑的地址不存在');
            }

            $addressModel->consignee = $validatedData['name'];
            $addressModel->phone = $validatedData['phone'];
            $addressModel->area = $validatedData['city'];
            $addressModel->address = $validatedData['address'];
            if(isset($validatedData['is_default'])){
                $addressModel->is_default = 1;
            }else{
                $addressModel->is_default = 0;
            }
            if($addressModel->save()){
                if($addressModel->is_default == 1){
                    $addressModel->where('user_id',$user_id)->where('id','!=',$addressModel->id)->where('is_default',1)->update(['is_default'=>0]);
                }
                return SelfResponse::success('修改地址成功','f_Wap_Address_myAddress');
            }

            return SelfResponse::error('修改地址失败');
        }else{
            return $this->wapRender('address.create_and_edit',[
                'page_title'=>'编辑地址',
                'addr'=>UserAddressModel::find($addr_id),
                'info'=>UserBaseModel::find($user_id)
            ]);
        }
    }

    public function delete($addr_id)
    {
        $user_id = \request()->input('user_id');
        $info = UserAddressModel::find($addr_id);
        if(!$info||$info->user_id!=$user_id){
            return SelfResponse::error('删除的地址不存在');
        }
        if(UserAddressModel::where('id',$addr_id)->where('user_id',$user_id)->delete()){
            return SelfResponse::success('地址删除成功','f_Wap_Address_myAddress');
        }
        return SelfResponse::error('地址删除失败');
    }

    public function setDefault(Request $request,$addr_id)
    {
        $user_id = $request->input('user_id');
        $info = UserAddressModel::find($addr_id);
        if(!$info||$info->user_id!=$user_id){
            return SelfResponse::error('操作的地址不存在');
        }

        if($info->is_default ==1){
            return SelfResponse::error('该地址已是默认地址,无须操作');
        }

        UserAddressModel::where('user_id',$user_id)->where('id',$addr_id)->update(['is_default'=>1]);
        UserAddressModel::where('user_id',$user_id)->where('id','!=',$addr_id)->update(['is_default'=>0]);

        return SelfResponse::success('设置成功','f_Wap_Address_myAddress');
    }
}